# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Zach Bacon <11doctorwhocanada@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=ffmpeg
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.3.1
pkgrel=7
pkgdesc="Complete and free Internet live audio and video broadcasting solution (mingw-w64)"
arch=('any')
url="https://ffmpeg.org/"
license=('GPL')
options=('staticlibs' 'strip')
depends=("${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-celt"
         "${MINGW_PACKAGE_PREFIX}-frei0r-plugins"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-gsm"
         "${MINGW_PACKAGE_PREFIX}-lame"
         "${MINGW_PACKAGE_PREFIX}-libass"
         "${MINGW_PACKAGE_PREFIX}-libbluray"
         "${MINGW_PACKAGE_PREFIX}-libcaca"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libmfx"
         "${MINGW_PACKAGE_PREFIX}-libmodplug"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-libvpx"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencore-amr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-opus"
         "${MINGW_PACKAGE_PREFIX}-rtmpdump"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-speex"
         "${MINGW_PACKAGE_PREFIX}-srt"
         "${MINGW_PACKAGE_PREFIX}-vulkan"
         "${MINGW_PACKAGE_PREFIX}-wavpack"
         "${MINGW_PACKAGE_PREFIX}-x264"
         "${MINGW_PACKAGE_PREFIX}-x265"
         "${MINGW_PACKAGE_PREFIX}-xvidcore"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-dlfcn"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-nasm")
source=(https://ffmpeg.org/releases/${_realname}-${pkgver}.tar.xz{,.asc}
        # From upstream:
        "0001-avutil-x86inc-fix-warnings-when-assembling-with-Nasm-2-15.patch"
        "0002-x86-h264_deblock-fix-warning-about-trailing-empty-parameter.patch"
        "0003-x86-vf_blend-fix-warnings-about-trailing-empty-parameters.patch"
        # MSYS2 patches:
        "0004-fix-srt.patch"
        "0005-Win32-Add-path-relocation-to-frei0r-plugins-search.patch")
validpgpkeys=('FCF986EA15E6E293A5644F10B4322F04D67658D8')
sha256sums=('ad009240d46e307b4e03a213a0f49c11b650e445b1f8be0dda2a9212b34d2ffb'
            'SKIP'
            '0dd56f6d4b206e1a689d3d656b60365138e29408b6ad02f76500764f89dde1b1'
            '6c20c2f7ab585e604efdaf2704bceb8a4f56a2722453ad818321b01f87293d99'
            'e9300ea624e3c10801607a1bb57fe0e2126cb42b48770828bd4421b7b58d6b96'
            '418a454c5d27f138bd8c56ca3c1991bdce1edee3a7fe5e1f8bc69a15abb99a89'
            'b32cad81226d8a72d99fc9cb509acb6a0533378bd351da65391945d7e0e81746')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  rm -f libavfilter/pathtools.{c,h} || true
  patch -p1 -i "${srcdir}/0001-avutil-x86inc-fix-warnings-when-assembling-with-Nasm-2-15.patch"
  patch -p1 -i "${srcdir}/0002-x86-h264_deblock-fix-warning-about-trailing-empty-parameter.patch"
  patch -p1 -i "${srcdir}/0003-x86-vf_blend-fix-warnings-about-trailing-empty-parameters.patch"
  patch -p1 -i "${srcdir}/0004-fix-srt.patch"
  patch -p1 -i "${srcdir}/0005-Win32-Add-path-relocation-to-frei0r-plugins-search.patch"
}

build() {
  # Fix using SRT headers
  CFLAGS+=" -DWIN32"
  CXXFLAGS+=" -DWIN32"

  local common_config
  common_config=(
    --disable-debug
    --enable-dxva2
    --enable-d3d11va
    --enable-fontconfig
    --enable-frei0r
    --enable-gnutls
    --enable-gpl
    --enable-libaom
    --enable-libass
    --enable-libbluray
    --enable-libcaca
    --enable-libcelt
    --enable-libdav1d
    --enable-libfreetype
    --enable-libgsm
    --enable-libmfx
    --enable-libmodplug
    --enable-libmp3lame
    --enable-libopencore_amrnb
    --enable-libopencore_amrwb
    --enable-libopenjpeg
    --enable-libopus
    --enable-librtmp
    --enable-libspeex
    --enable-libsrt
    --enable-libtheora
    --enable-libvorbis
    --enable-libx264
    --enable-libx265
    --enable-libxvid
    --enable-libvpx
    --enable-libwebp
    --enable-libxml2
    --enable-openal
    --enable-libwavpack
    --enable-pic
    --enable-postproc
    --enable-runtime-cpudetect
    --enable-swresample
    --enable-version3
    --enable-vulkan
    --enable-zlib
    --disable-doc
  )
  for _variant in -static -shared; do
    [[ -d "${srcdir}/build-${MINGW_CHOST}${_variant}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}${_variant}"
    mkdir -p "${srcdir}/build-${MINGW_CHOST}${_variant}" && cd "${srcdir}/build-${MINGW_CHOST}${_variant}"
    if [[ ${_variant} == -static ]]; then
      ENABLE_VARIANT=--enable-static
    else
      ENABLE_VARIANT=--enable-shared
    fi
    CC=${MINGW_PREFIX}/bin/gcc \
    CXX=${MINGW_PREFIX}/bin/g++ \
    ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --target-os=mingw32 \
      --arch=${CARCH%%-*} \
      "${common_config[@]}" \
      --logfile=config.log \
      ${ENABLE_VARIANT} ||
      {
        cat config.log
        exit 1
      }
    make VERBOSE=1
  done
}

check() {
  for _variant in -static -shared; do
    cd "${srcdir}/build-${MINGW_CHOST}${_variant}"
    # workaround for conflict with SDL main(), use it if you have SDL installed
    # make check CC_C="-c -Umain"
    make check VERBOSE=1 || true
  done
}

package() {
  for _variant in -static -shared; do
    cd "${srcdir}/build-${MINGW_CHOST}${_variant}"
    make DESTDIR="${pkgdir}" install
  done
  
  rm -f ${pkgdir}/${MINGW_PREFIX}/lib/*.def
  rm -f ${pkgdir}/${MINGW_PREFIX}/bin/*.lib

  #find ${pkgdir}${MINGW_PREFIX}/bin -type f -name "*.exe" -exec objcopy --subsystem console {} \;

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  find ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig -name *.pc -exec sed -i -e"s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \;
}
